package com.gaobug.base.config.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hzq
 * @date 2022/3/21
 */
@Slf4j
public class RabbitMqFactory {
    private final RabbitAdmin rabbitAdmin;
    public final Map<String, RabbitMqProperties.QueueConfig> queueConfigMap;

    public RabbitMqFactory(RabbitAdmin rabbitAdmin, Map<String, RabbitMqProperties.QueueConfig> queueConfigMap) {
        this.rabbitAdmin = rabbitAdmin;
        this.queueConfigMap = queueConfigMap;
    }

    public void assemblyQueue() {
        log.info("-----------------RabbitMQ-----------------");
        if (ObjectUtils.isEmpty(queueConfigMap)) {
            log.error("queueConfigMap is null");
            return;
        }
        for (Map.Entry<String, RabbitMqProperties.QueueConfig> entry : queueConfigMap.entrySet()) {
            RabbitMqProperties.QueueConfig config = entry.getValue();
            // 根据交换机模式 生成不同的交换机
            if (ExchangeTypes.TOPIC.equals(config.getExchangeType())) {
                TopicExchange topic = new TopicExchange(config.getExchangeName(), config.isDurable(), config.isAutoDelete());
                rabbitAdmin.declareExchange(topic);
            } else if (ExchangeTypes.FANOUT.equals(config.getExchangeType())) {
                FanoutExchange fanout = new FanoutExchange(config.getExchangeName(), config.isDurable(), config.isAutoDelete());
                rabbitAdmin.declareExchange(fanout);
            } else if (ExchangeTypes.DIRECT.equals(config.getExchangeType())) {
                DirectExchange direct = new DirectExchange(config.getExchangeName(), config.isDurable(), config.isAutoDelete());
                rabbitAdmin.declareExchange(direct);
            }
            // 附加参数
            Map<String, Object> args = null;
            if (StringUtils.hasText(config.getDeadExchangeName())) {
                // 绑定附加的死信交换机
                args = new HashMap<>(2);
                args.put("x-dead-letter-exchange", config.getDeadExchangeName());
            }
            // 创建队列
            Queue queue = new Queue(config.getQueueName(), config.isDurable(), config.isExclusive(), config.isAutoDelete(), args);
            rabbitAdmin.declareQueue(queue);
            // 队列绑定交换机
            rabbitAdmin.declareBinding(new Binding(
                    // queue名称
                    config.getQueueName(),
                    Binding.DestinationType.QUEUE,
                    // exchange名称
                    config.getExchangeName(),
                    // queue的routingKey
                    config.getQueueName(),
                    // 绑定的参数
                    null));
            log.info("create queue {} success", config.getQueueName());
        }
        log.info("-----------------RabbitMQ-----------------");
    }
}
